﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Xml.Linq;

namespace BaseTypesLib.Model.Backgrounds
{
    public enum ImageRepeat { None, Both, ScaleToFit }
    public enum PositionX { Left, Center, Right }
    public enum PositionY { Top, Center, Bottom }

    public class ImageBackground:Background
    {
        private string imgPath, projPath;
        private PositionX posx;
        private PositionY posy;
        private ImageRepeat repeat;

        public string ImagePath
        {
            get { return imgPath; }
            set
            {
                if (imgPath != value)
                {
                    imgPath = value;
                    OnPropertyChanged("ImagePath");
                }
            }

        }
        [Browsable(false)]
        public string ProjectPath
        {
            get { return projPath; }
            set
            {
                if (projPath != value)
                {
                    projPath = value;
                    OnPropertyChanged("ProjectPath");
                }
            }
        }
        public PositionX PositionX
        {
            get { return posx; }
            set
            {
                if (posx != value)
                {
                    posx = value;
                    OnPropertyChanged("PositionX");
                }
            }
        }
        public PositionY PositionY
        {
            get { return posy; }
            set
            {
                if (posy != value)
                {
                    posy = value;
                    OnPropertyChanged("PositionY");
                }
            }
        }
        public ImageRepeat Repeat
        {
            get { return repeat; }
            set
            {
                if (repeat != value)
                {
                    repeat = value;
                    OnPropertyChanged("Repeat");
                }
            }
        }
        public override string ToString()
        {
            return "Image";
        }
        public override XElement GetXml()
        {
            XElement elem = base.GetXml();
            elem.SetAttributeValue("Repeat", Repeat);
            elem.SetAttributeValue("PosX", PositionX);
            elem.SetAttributeValue("PosY", PositionY);
            if (ImagePath != null && ImagePath.Trim().Length > 0)
                elem.SetAttributeValue("ImagePath", ImagePath.Trim());
            return elem;
        }
        protected override void FromXmlElement(XElement elem)
        {
            base.FromXmlElement(elem);
            var r = elem.Attributes("Repeat").FirstOrDefault();
            if (r != null)
                Repeat = (ImageRepeat)Enum.Parse(typeof(ImageRepeat), r.Value);

            var px = elem.Attributes("PosX").FirstOrDefault();
            if (px != null)
                PositionX = (PositionX)Enum.Parse(typeof(PositionX), px.Value);

            var py = elem.Attributes("PosY").FirstOrDefault();
            if (py != null)
                PositionY = (PositionY)Enum.Parse(typeof(PositionY), py.Value);

            var path = elem.Attributes("ImagePath").FirstOrDefault();
            if (path != null)
                ImagePath = path.Value;

        }
    }
}
